/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.OutletManagement;

import entity.Outlet;
import session.TX.AreaSBRemote;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import session.TX.OutletSBRemote;
import entity.OutletSeatArea;
import entity.PurchasedItem_GroupOn;
import entity.PurchasedItem_Regular;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.TX.TransactionManagerSBRemote;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean (name="outletManagerIOTRManagedBean")
@ViewScoped
public class OutletManagerIOTRMB {

    @EJB
    private OutletSBRemote outletBean;
    @EJB
    private AreaSBRemote areaBean;
    
    @EJB
    private TransactionManagerSBRemote manager;
    
    private String stakeholderName;
    private Integer phoneNumber;
    private String noShowPenaltyString;
    private String autoConfirmReservation;
    private String leadTime_DayString;
    private String bookingWindowString;
    private String statusMessage;
    private Outlet outlet;
    private String customerEmail;
    private int noToRedeem;
    
    /** Creates a new instance of OutletManagerIOTRMB */
    public OutletManagerIOTRMB() {
    }

    public void saveNewGeneralInfo(ActionEvent event) {
        final Boolean autoConfirm = Boolean.valueOf(autoConfirmReservation);
        final Integer leadTime_Day = Integer.valueOf(this.leadTime_DayString);
        final Integer bookingWindow = Integer.valueOf(this.bookingWindowString);
        final Double noShowPenalty = Double.valueOf(noShowPenaltyString);
        System.out.println(autoConfirm);
        System.out.println(leadTime_Day);
        System.out.println(bookingWindow);
        System.out.println(noShowPenalty);
        statusMessage = outletBean.updateGeneralInfo(retrieveFromSessionOutletID(), noShowPenalty, autoConfirm, leadTime_Day, bookingWindow);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, getStatusMessage(), ""));
    }
    
    public List<PurchasedItem_Regular> getPurchasedItem(){
        Long id = Long.parseLong(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletId").toString());
        return manager.retrievePurchasedItemRegular(customerEmail, id, true);
    }
    
    public List<PurchasedItem_GroupOn> getGrouponItem(){
        String name = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString();
        return manager.retrievePurchasedItemGroupon(customerEmail, name, true);
    }
    
    public void redeemPurchases(Long id){
        manager.redeemPurchases(id);
    }
    
    public void redeemGroupon(Long id){
        
        System.out.println(">>> Attempting to redeem group deal item: " + id + ", no. of coupons: " + noToRedeem);
        boolean status = manager.redeemGroupon(id, noToRedeem);
        if(status == true){
            statusMessage = " Redemption for Group Buying deal is Successful";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Result: " + statusMessage, ""));
        }else{
            statusMessage = " Redemption for Group Buying deal is not Successful";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Result: " + statusMessage, ""));
        }
    }

    public Outlet getOutlet() {
        System.out.println(retrieveFromSessionOutletID());
        if (this.outlet == null) {
            Long outletId = retrieveFromSessionOutletID();;
            if (outletId == null) {
                return null;
            } else {
                return outletBean.getOutlet(outletId);
            }
        }
        return outlet;
    }

    public void setOutlet(Outlet o) {
        this.outlet = o;
    }

    public String getAutoConfirmReservation() {
        Boolean autoConfirmReservation = this.getOutlet().getAutoConfirmReservation();
        if (autoConfirmReservation == null)
            return null;
        else 
            return autoConfirmReservation.toString();
    }

    public void setAutoConfirmReservation(String autoConfirmReservation) {
        this.autoConfirmReservation = autoConfirmReservation;
    }

    public String getNoShowPenalty() {
        Long outletId = retrieveFromSessionOutletID();
        if (outletId == null){
            return null;
        }
        Double penalty = outletBean.getNoShowPenalty(outletId);
        if (penalty == null) {
            return null;
        }
        return penalty.toString();
    }

    public void setNoShowPenalty(String noShowPenalty) {
        this.noShowPenaltyString = noShowPenalty;
    }

    public Integer getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(Integer phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getStakeholderName() {
        return stakeholderName;
    }

    public void setStakeholderName(String stakeholderName) {
        this.stakeholderName = stakeholderName;
    }

    public String getLeadTime_Day() {
        Long outletId = retrieveFromSessionOutletID();
        if (outletId == null){
            return null;
        }
        Integer leadTime = outletBean.getLeadTime(outletId);
        if (leadTime == null) {
            return null;
        }
        return leadTime.toString();
    }

    public void setLeadTime_Day(String leadTime_Day) {
        this.leadTime_DayString = leadTime_Day;
    }

    public String getBookingWindow() {
        Long outletId = retrieveFromSessionOutletID();
        if (outletId == null){
            return null;
        }
        Integer bookingWindow = outletBean.getBookingWindow(outletId);
        if (bookingWindow == null) {
            return null;
        }
        return bookingWindow.toString();
    }

    public void setBookingWindow(String bookingWindow) {
        this.bookingWindowString = bookingWindow;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public List<OutletSeatArea> getAreas() {
        Long outletId = retrieveFromSessionOutletID();
        if (outletId == null){
            return null;
        }
        return areaBean.getAllAreas(outletId);
    }

    public String getOutletName() {
        return FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletName").toString();
    }
    
    public String getCustomerEmail() {
        return customerEmail;
    }

    public void setCustomerEmail(String customerEmail) {
        this.customerEmail = customerEmail;
    }
    
    
    
    
    
    //Session Attribute Codes
    //Randall: Added this method to pull out the outlet ID from session
    public Long retrieveFromSessionOutletID(){
        return (Long)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletId");
    }
    
    //End Session Attribute Codes
    //======================================================================================================

    /**
     * @return the noToRedeem
     */
    public int getNoToRedeem() {
        return noToRedeem;
    }

    /**
     * @param noToRedeem the noToRedeem to set
     */
    public void setNoToRedeem(int noToRedeem) {
        this.noToRedeem = noToRedeem;
    }

}
